package com.zhiwang.planet.mvvm.binding

import android.graphics.Color
import android.util.Log
import android.view.View
import android.widget.TextView
import androidx.annotation.ColorRes
import androidx.core.content.ContextCompat
import androidx.databinding.BindingAdapter
import com.zhiwang.planet.BuildConfig
import com.zhiwang.planet.util.checkedTextColor
import com.zhiwang.planet.util.enabledTextColor
import com.zhiwang.planet.util.pressedTextColor


fun View?.color(@ColorRes res: Int): Int {
    return if (this == null) {
        if (BuildConfig.DEBUG)
            Log.e(BuildConfig.APPLICATION_ID, "color: cannot get color from a null view ")
        Color.BLACK
    } else ContextCompat.getColor(context, res)
}

@BindingAdapter("pressed_textColor", "unpressed_textColor")
fun setTextPressStateTextColor(textView: TextView, pressedTextColor: Int, normalColor: Int) {

    textView.pressedTextColor(normalColor, pressedTextColor)
}

@BindingAdapter("pressed_textColorRes", "unpressed_textColorRes")
fun setTextPressStateTextColorRes(textView: TextView, pressedTextColor: Int, normalColor: Int) {

    textView.pressedTextColor(textView.color(normalColor), textView.color(pressedTextColor))
}


@BindingAdapter("checked_textColor", "unchecked_textColor")
fun setCheckedStateTextColor(textView: TextView, checkedTextColor: Int, uncheckedTextColor: Int) {
    textView.checkedTextColor(checkedTextColor, uncheckedTextColor)
}

@BindingAdapter("checked_textColorRes", "unchecked_textColorRes")
fun setCheckedStateTextColorRes(textView: TextView, checkedTextColor: Int, uncheckedTextColor: Int) {
    textView.checkedTextColor(textView.color(checkedTextColor), textView.color(uncheckedTextColor))
}

@BindingAdapter("enabled_textColor", "disabled_textColor")
fun setEnableStateTextColor(textView: TextView, enabledTextColor: Int, disabledTextColor: Int) {
    textView.enabledTextColor(disabledTextColor, enabledTextColor)
}

@BindingAdapter("enabled_textColorRes", "disabled_textColorRes")
fun setEnableStateTextColorRes(textView: TextView, enabledTextColor: Int, disabledTextColor: Int) {
    textView.enabledTextColor(textView.color(disabledTextColor), textView.color(enabledTextColor))
}